package com.wsg.lightaccounting.PopupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wsg.lightaccounting.Activity.CashCardActivity;
import com.wsg.lightaccounting.Activity.MainActivity;
import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.R;

/**
 * Created by dell-pc on 2016/11/22.
 */

public class CashupdatePopupWindow extends PopupWindow implements View.OnClickListener,View.OnKeyListener{
    private View mMenuView;
    private ImageButton add,back;
    private EditText mark;
    private Context context;
    public CashupdatePopupWindow(final Activity context, int w, int h){
        super(context);
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        mMenuView = inflater.inflate(R.layout.mark_layout, null);
        add = (ImageButton) mMenuView.findViewById(R.id.add);
        back = (ImageButton) mMenuView.findViewById(R.id.back);
        mark = (EditText) mMenuView.findViewById(R.id.add_content);
        mark.setFocusable(true);
        mark.setOnKeyListener(this);
        mark.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        back.setOnClickListener(this);
        this.setContentView(mMenuView);
        add.setOnClickListener(this);
        this.setWidth(w);
        this.setHeight(h);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        this.setBackgroundDrawable(dw);
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
               action();
                dismiss();
                break;
            case R.id.back:
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        CashCardActivity.changeInput(mark,context,false);
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
           action();
            dismiss();
            return true;
        }
        return false;
    }

    private void action(){
        float balance = 0f;
        if (!String.valueOf(mark.getText()).equals("")){
            balance = Float.parseFloat(String.valueOf(mark.getText()));
        }
        AccountManager.getAccountManager(context).updateCashAccount(balance);
        MainActivity.updateAllUI(context);
    }
    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        CashCardActivity.changeInput(mark,context,true);
    }
}
